import { rayMix } from '../math/arrayMix.js'
import { _dis, _mid, _rotate2, _o, _moveDis } from '../math/index.js'
import { _path } from './path.js'
import { linkPoints, linkCrossPoints, edgeExtensionPoints, edgeExtensionCrossPoints } from '../algorithm/link.js'
import { _polyline } from './curve.js'
import { _merge } from '../common/index.js'
import { _pathMode } from './pathMode.js';
import { genLabel } from '../common/labels.js'


let propsMap = {
    stroke: 'strokeStyle',
    fill: 'fillStyle',
    strokeWidth: 'lineWidth',
    fillAlpha: 'globalAlpha',
    fillOpacity: 'globalAlpha',
    opacity: 'globalAlpha',
    alpha: 'globalAlpha'
}
let valMap = {
    none: 'transparent'
}
// 虚线
const _dashLine = (ctx, key, value) => {
    if ('strokeDasharray' === key && value > 0) {
        ctx.setLineDash([value, value]);
        return true
    }
}

// 属性
export const _props = (ctx, props = {}) => {
    Object.keys(props).forEach(t => {
        let value = props[t]
        if (!_dashLine(ctx, t, value)) {
            let prop = propsMap[t] || t
            ctx[prop] = valMap[value] || value
        }
    })
}
// 文本
export const _text = (ctx, text, pos, props = {}) => {
    ctx.save()
    let { fontSize = 12, stroke = 'red' } = props
    ctx.font = `${fontSize}px 微软雅黑`
    ctx.textAlign = 'center'
    ctx.textBaseline = 'center'
    ctx.fillStyle = stroke;
    ctx.fillText(text, pos[0], pos[1])
    ctx.restore()
}
// 多文本
export const _texts = (ctx, points, labels) => {
    let { type } = labels
    let o = _o(points)
    points.forEach((t, i) => {
        let pos = _moveDis(t, o, 10)
        let text = genLabel(type, i)
        _text(ctx, text, pos, labels)
    });
}
// 圆
export const _circle = (ctx, o, r, props = {}) => {
    ctx.save()
    let [x, y] = o
    let { stroke, fill } = props
    ctx.beginPath();
    _props(ctx, props)
    ctx.arc(x, y, r, 0, 2 * Math.PI);
    if (stroke && stroke !== 'none') {
        ctx.stroke();
    }
    if (fill && fill !== 'none') {
        ctx.fill()
    }
    ctx.closePath();
    ctx.restore()
}

// 多个圆
export const _circles = (ctx, points, options = {}) => {
    let { r = 5 } = options
    points.forEach((t, i) => {
        _circle(ctx, t, r, {
            stroke: 'red',
            ...options
        })
    });
}

// 顶点
export const _vertex = (ctx, points, options = {}) => {
    let { r = 5 } = options
    let { one } = options
    if (one) {
        _circle(ctx, points[0], r, {
            stroke: 'red',
            ...options
        })
    } else {
        _circles(ctx, points, {
            r,
            ...options
        })
    }

}
// 中心点
export const _centre = (ctx, o, options = {}) => {
    let { r = 5 } = options
    _circle(ctx, o, r, {
        fill: 'none',
        stroke: 'red',
        ...options
    })
}


// 半径
export const _radius = (ctx, { points }, options) => {
    let { one } = options
    let o = _o(points)
    if (one) {
        _path(ctx, { points: [o, points[0]], closed: false }, options)
    } else {
        let rps = rayMix(o, points)
        _path(ctx, { points: rps, closed: false }, options)
    }

}

// 内切圆
export const _incircle = (ctx, { o, points }, options) => {
    let r = _dis(o, _mid.apply(null, points.slice(0, 2)))
    _circle(ctx,
        o, r
        , {
            fill: 'none',
            stroke: 'red',
            ...options
        })
}
// 外切圆
export const _excircle = (ctx, { o, r }, options) => {
    _circle(ctx,
        o, r
        , {
            fill: 'none',
            stroke: 'red',
            ...options
        })
}

// 边
export const _edge = (ctx, options, props) => {
    // _props(ctx, props)
    // _path(ctx, { points, closed: true }, props)
    let { pathMode, points, curve, closed } = options
    ctx.save()
    ctx.beginPath();
    _props(ctx, props)
    _pathMode(ctx, { pathMode, points, curve, closed }, props)
    ctx.restore()
}

// 链接线
export const _links = (ctx, points, options) => {
    let lps = linkPoints(points)

    _path(ctx, { points: lps, closed: false }, options)

    if (options.crossPoints) {
        let lcps = linkCrossPoints(points)
        _circles(ctx, lcps, {
            fill: 'red',
            ...options
        })
    }
}

// const _dashLines = (ctx,points, props) => {
//     return _path(_lines(points), props)
// }


// 边延长线
export const _edgeExtension = (ctx, { points, edgeExtension }, props) => {
    let { crossPoints, labels } = edgeExtension
    let ps = edgeExtensionPoints(points, edgeExtension.iterNum)

    ctx.save()
    ctx.beginPath()
    _props(ctx, props)
    // _props(ctx, { strokeDasharray: 4 })
    _polyline(ctx, { points: ps, step: 2 })
    ctx.stroke()


    // gs.push(_dashLines(ps, { ...props, ...edgeExtension }))
    // gs.push(_path(_points(ps, edgeExtension.r), _merge(props, edgeExtension)))

    if (crossPoints || labels) {
        let cps = edgeExtensionCrossPoints(points)
        // 交点
        if (crossPoints) {
            _circles(ctx, cps, {
                stroke: 'gray',
                fill: 'white',
                ..._merge(props, edgeExtension)
            })
            // gs.push(_path(_points(cps, edgeExtension.r), _merge(props, edgeExtension)))
        }

        // 标注
        if (labels) {
            _texts(ctx, cps, {
                stroke: 'blue'
            })
        }
    }
    ctx.restore()
}